package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.service.AccountService;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.query.PaymentScheduleQueryObject;
import com.xmg.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2021.4.16 08:46
 * @Description: 还款控制器
 * @since JDK 1.8
 */
@Controller
public class ReturnMoneyController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private AccountService accountService;

    /**
     * 进入还款信息明细页面
     * @param qo
     * @param model
     * @return
     */
    @RequireLogin
    @RequestMapping("/borrowBidReturn_list")
    public String borrowBidReturnList(@ModelAttribute("qo") PaymentScheduleQueryObject qo, Model model){
        // 获取当前用户的账号信息
        Account account = this.accountService.getById(UserContext.getCurrent().getId());
        qo.setLogininfoId(UserContext.getCurrent().getId());  // 设置条件为当前用户
        model.addAttribute("pageResult",this.bidRequestService.queryForPaymentSchedule(qo));
        model.addAttribute("account",account);
        return "returnmoney_list";
    }

    @ResponseBody
    @RequestMapping("/returnMoney")
    public JSONResult returnMoney(Long id){
        JSONResult json = new JSONResult();
        try {
            this.bidRequestService.returnMoney(id);
        } catch (Exception e) {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
